var main = angular.module('main');

main.controller('edit_apiCtrl', ['$scope', 'http', '$stateParams', '$state', function($scope, http, $stateParams, $state) {
    $scope.api_id = $stateParams.api_id;
    $scope.project_id = $stateParams.project_id;
    $scope.finish = "unfinished";

    $scope.desc = "";
    $scope.json = "";
    $scope.Params = [];
    $scope.Returns = [];
    $scope.Status = [];
    $scope.add = {};
    $scope.add.name = '';
    $scope.add.type = 'string';
    $scope.add.required = 'true';
    $scope.add.returnName = '';
    $scope.add.returnType = 'string';
    $scope.add.restriction = '';
    $scope.add.remark = '';
    $scope.add.statusCode = '';
    $scope.add.desc = '';
    $scope.apiDetail = {};

    http.post('/api/group/list', { project_id: $scope.project_id }).then(function(data) {
        $scope.groups = data.data;
    });

    http.get('/api/method/list').then(function(data) {
        $scope.methods = data.data;
    });

    $scope.types = [];
    http.get('/api/type/list').then(function(data) {
        $scope.types = data.data;
        $scope.add.type = $scope.types[0];
        $scope.add.returnType = $scope.types[0];
    });

    $scope.methodSelected = [];
    http.post('/api/detail', { api_id: $scope.api_id }).then(function(data) {
        $scope.apiDetail = data.data;
        $scope.groupSelected = $scope.apiDetail.group_id;
        for (var i = 0; i < $scope.apiDetail.method.length; i++) {
            var index = $scope.apiDetail.method[i];
            $scope.methodSelected.push($scope.apiDetail.method[i]);
            $scope.methods[index].selected = true;
        }
        $scope.Params = $scope.apiDetail.request;
        $scope.Returns = $scope.apiDetail.output;
        $scope.Status = $scope.apiDetail.status;
        $scope.json = $scope.apiDetail.output_example;
        $scope.desc = $scope.apiDetail.description;
        if ($scope.apiDetail.finished) {
            $scope.finish = "finished";
        }
    });

    $scope.toggleSelection = function(method) {
        var index = $scope.methodSelected.indexOf(method.value);
        if (index > -1)
            $scope.methodSelected.splice(index, 1);
        else
            $scope.methodSelected.push(method.value);
    };


    $scope.deleteParam = function(name) {
        for (var i = 0; i < $scope.Params.length; i++) {
            if ($scope.Params[i].name == name) {
                $scope.Params.splice(i, 1);
                break;
            }
        }
    };
    $scope.addParam = function() {
        var newParam = { 'name': $scope.add.name, 'type': $scope.add.type.value, 'stype': $scope.add.type, 'restrict': $scope.add.restriction, 'remark': $scope.add.remark };
        if ($scope.add.required == 'true') {
            newParam.necessary = true;
            newParam.required = "是";
        } else {
            newParam.necessary = false;
            newParam.required = "否";
        }
        $scope.Params.push(newParam);
        $scope.add.name = '';
        $scope.add.restriction = '';
        $scope.add.remark = '';
    };
    $scope.deleteReturn = function(name) {
        for (var i = 0; i < $scope.Returns.length; i++) {
            if ($scope.Returns[i].name == name) {
                $scope.Returns.splice(i, 1);
                break;
            }
        }
    };
    $scope.addReturn = function() {
        var newReturn = { 'name': $scope.add.returnName, 'type': $scope.add.returnType.value, 'stype': $scope.add.returnType, 'remark': $scope.add.returnRemark };
        $scope.Returns.push(newReturn);
        $scope.add.returnName = '';
        $scope.add.returnRemark = '';
    };
    $scope.deleteStatus = function(code) {
        for (var i = 0; i < $scope.Status.length; i++) {
            if ($scope.Status[i].statusCode == code) {
                $scope.Status.splice(i, 1);
                break;
            }
        }
    };
    $scope.addStatus = function() {
        var newS = { 'code': $scope.add.statusCode, 'remark': $scope.add.desc };
        $scope.Status.push(newS);
        $scope.add.statusCode = '';
        $scope.add.desc = '';
    };

    $scope.submit = function() {
        if ($scope.url === "")
            UIkit.modal.alert('url不能为空');
        if ($scope.methodSelected.length===0)
            UIkit.modal.alert('访问方法不能为空');
        else {
            var postParam = {};
            postParam.api_id = $scope.api_id;
            postParam.method = JSON.stringify($scope.methodSelected);
            postParam.group_id = $scope.groupSelected;
            postParam.description = $scope.desc;
            postParam.output_example = $scope.json;
            postParam.request = JSON.stringify($scope.Params);
            postParam.output = JSON.stringify($scope.Returns);
            postParam.status = JSON.stringify($scope.Status);
            if ($scope.finish === "finished") {
                postParam.finished = true;
            } else
                postParam.finished = false;
            http.post('/api/update', postParam).then(function(data) {
                UIkit.modal.alert("API修改保存成功！");
                $state.go('project.apis', { 'project_id': $scope.project_id });
            });
        }
    };
}]);
